﻿using System;
using System.Collections.Generic;
using System.Data;

using System.Text;
using WinnerCMS.Common;
using WinnerCMS.Model;

using System.Linq;
using System.Data.Entity;
namespace WinnerCMS.Data
{
    /// <summary>
    /// DAL_Announce 的摘要说明。
    /// </summary>
    public class AnnounceDal
    {

        #region 添加公告
        /// <summary>
        /// 添加公告
        /// </summary>
        /// <param name="model"></param>
        public void Add(Announce model)
        {
            using (Entities db = new Entities())
            {
                db.Announces.Add(model);
                db.SaveChangesAsync().Wait();

            }
        }
        #endregion

        #region 修改公告
        /// <summary>
        /// 修改公告
        /// </summary>
        /// <param name="model"></param>
        public void Edit(Announce model)
        {
            using (Entities db = new Entities())
            {
                db.Entry(model).State = EntityState.Modified;
                db.SaveChangesAsync().Wait();

            }
        }
        #endregion

        #region 删除公告
        /// <summary>
        /// 删除公告
        /// </summary>
        /// <param name="Ids"></param>
        public void Delete(int[] Ids)
        {
            using (Entities db = new Entities())
            {
                var q = db.Announces.Where(x => Ids.Contains(x.Id));

                foreach (var v in q)
                {
                    db.Announces.Remove(v);
                }

                db.SaveChangesAsync().Wait();
            }
        }

        #endregion

        #region 加载公告
        /// <summary>
        /// 加载公告
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Announce GetInfo(int id)
        {
            using (Entities db = new Entities())
            {
                return db.Announces.FirstOrDefault(x => x.Id == id);
            }
        }

        /// <summary>
        /// 前台显示
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Announce Show(int id)
        {
            using (Entities db = new Entities())
            {
                return db.Announces.FirstOrDefault(x => x.Id == id && x.Pass);
            }
        }

        #endregion

        /// <summary>
        /// 通过审核或者弃审
        /// </summary>
        /// <param name="id"></param>
        public Announce PassOrUnpass(int id)
        {
            using (Entities db = new Entities())
            {
                var value = db.Announces.FirstOrDefault(x => x.Id == id);
                if (value != null)
                {
                    value.Pass = !value.Pass;
                    db.SaveChangesAsync().Wait();

                    return value;
                }
                return null;
            }

        }

        /// <summary>
        /// 公告列表－－前台使用
        /// </summary>
        /// <returns></returns>
        public IList<Announce> BindList(int top)
        {
            using (Entities db = new Entities())
            {
                return db.Announces.Where(x => x.Pass).OrderByDescending(x => x.UpdateTime).Take(top).ToArray();
            }
        }


        /// <summary>
        /// 绑定数据
        /// </summary>
        /// <param name="pass"></param>
        /// <param name="pp"></param>
        /// <returns></returns>
        public IList<Announce> BindList(bool? pass, PagingParam pp)
        {
            using (Entities db = new Entities())
            {
                var query = from x in db.Announces select x;
                if (pass != null)
                {
                    query = query.Where(x => x.Pass == pass.Value);
                }

                pp.RecordCount = query.Count();

                return query.OrderByDescending(x => x.UpdateTime).Skip(pp.Skip).Take(pp.PageSize).ToArray();
            }
        }
    }
}